﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using SdmxMl.Common;
using SdmxMl.Helper;
using SdmxMl.Manager;

namespace SdmxMl.Structure
{
    public class BaseTarget : IAnnotationList
    {
        public string Urn { get; set; }
        public string Uri { get; set; }
        public string Id { get; set; }
        public AnnotationList AnnotationList { get; set; }

        public BaseTarget()
        {
            AnnotationList = new AnnotationList();
        }

        protected void ReadBasic(XmlNode node, SdmxNamespaceManager nsmgr)
        {
            if (node != null)
            {
                Urn = Xmlhelper.GetAtt(node, "urn");
                Uri = Xmlhelper.GetAtt(node, "uri");

                string query = "./common:Annotations/common:Annotation";
                XmlNodeList nodeList = node.SelectNodes(query, nsmgr);
                foreach (XmlNode na in nodeList)
                {
                    Annotation a = new Annotation(na, nsmgr);
                    AnnotationList.Add(a);
                }
            }
        }
    }

        
    

    /// <summary>
    /// KeyDescriptorValuesTarget is target object which references a data key for the purpose of
    /// attach reference metadata to portions of data. A data key is a set of dimension references
    /// and values for those dimension. This component on its own is not of much use, as the data
    /// key only has local references to the dimensions. Therefore it is typical that this is used
    /// in combination with some sort of reference to the data (either a data set reference or a
    /// reference to the underlying structure, structure usage, or provision agreement of the data.
    /// </summary>
    public class KeyDescriptorValuesTargetType : BaseTarget
    {
        public KeyDescriptorValuesTargetType()
        {
        }

        public KeyDescriptorValuesTargetType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
        {
            XmlNode node = nodeArtefact.SelectSingleNode("./structure:KeyDescriptorValuesTarget", nsmgr);
            ReadBasic (node, nsmgr);
        }

    }

    /// <summary>
    /// DataSetTarget is target object which references a data set for the purpose of attach reference
    /// metadata data. A data set reference is a full reference to a data provider and an identifier
    /// for the data set.
    /// </summary>
    public class DataSetTargetType : BaseTarget
    {
        public DataSetTargetType()
        {
        }

        public DataSetTargetType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
        {
            XmlNode node = nodeArtefact.SelectSingleNode("./structure:DataSetTarget", nsmgr);
            ReadBasic (node, nsmgr);
        }

    }

    /// <summary>
    /// ReportPeriodTargetType defines the structure of a report period target object. The report period
    /// target object has a fixed representation and identifier.
    /// </summary>
    public class ReportPeriodTargetType : BaseTarget
    {
        private TimeDataType TextType { get; set; }
        public string StartTime { get; set; }
        public string EndTime { get; set; }

        public ReportPeriodTargetType()
        {
            TextType = TimeDataType.ObservationalTimePeriod;
        }

        public ReportPeriodTargetType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
        {
            TextType = TimeDataType.ObservationalTimePeriod;

            XmlNode node = nodeArtefact.SelectSingleNode("./structure:ReportPeriodTarget", nsmgr);
            ReadBasic (node, nsmgr);

            //Representation
            if (node != null)
            {
                string query = "structure:LocalRepresentation/structure:TextFormat";
                XmlNode nodeRep = node.SelectSingleNode(query, nsmgr);
                if (nodeRep != null)
                {
                    string tt = Xmlhelper.GetAtt(nodeRep, "textType");

                     foreach (TimeDataType ttype in Enum.GetValues(typeof(TimeDataType)))
                     {
                         if (tt == ttype.ToString())
                         {
                             TextType = ttype;
                             break;
                         }
                     }

                     StartTime = Xmlhelper.GetAtt(nodeRep, "startTime");
                     EndTime = Xmlhelper.GetAtt(nodeRep, "endTime");
                }
            }
        }
    }

}
